﻿@model XStudio.Service.Timing.Entity.TimingServiceEntity
@using XStudio.Core.Ext
@{ var config = (XStudio.Service.Timing.ScheduleConfig)ViewBag.ScheduleConfig;}
<script type="text/javascript">

    $(function () {
        var frequencyType = 0;
        Frequency_Click(frequencyType);
        DayFrequency_Click($("#radDayCycle").attr("checked"));
        EndDate_Click($("#radEndDate").attr("checked"));

        //保存按钮点击事件
        $("#btnSave").click(function () {
            if (!Validate()) {
                return false;
            }

            $.ajax({
                url: $('#frmInput').attr("action"),
                data: $('#frmInput').serialize(),
                type: "Post",
                error: function (request) {
                    alert('保存定时服务出错，请重试！');
                },
                success: function (data) {
                    if (data.IsOK) {
                        $.closeWin($("#btnSave"));
                        
                        //更新列表
                        $('#tbTimingList').datagrid('reload');
                    }
                    else
                        alert(data.Description);
                }
            });
            //            var elements = $("input");
            //            for (var i = 0; i < elements.length; i++) {
            //                elements[i].disabled = false;
            //            }
            //            var elements = $("select");
            //            for (var i = 0; i < elements.length; i++) {
            //                elements[i].disabled = false;
            //            }
        });
    });

    //发生频率点击事件
    function Frequency_Click(type) {
        var phlDay = $("#phlDay");
        var phlWeek = $("#phlWeek");
        var phlMonth = $("#phlMonth");
        phlDay.hide();
        phlWeek.hide();
        phlMonth.hide();
        switch (parseInt(type)) {
            case 1:
                phlWeek.show();
                break;
            case 2:
                phlMonth.show();
                var isRelative = $("#radRelative").attr("checked");
                Relative_Click(isRelative);
                break;
            default:
                phlDay.show();
                break;
        }
    }

    //月是否相对时间
    function Relative_Click(isRelative) {
        $("#txtMonthInterval").attr("disabled", isRelative);
        $("#txtMonthDay").attr("disabled", isRelative);

        $("#dropRelative").attr("disabled", !isRelative);
        $("#txtMonthInterval2").attr("disabled", !isRelative);
        $("#dropWeenInMonth").attr("disabled", !isRelative);
    }

    //日频率类型点击
    function DayFrequency_Click(isCycle) {
        $("#dropDayOnceHour").attr("disabled", isCycle);
        $("#dropDayOnceMinute").attr("disabled", isCycle);

        $("#txtInterval").attr("disabled", !isCycle);
        $("#dropUnits").attr("disabled", !isCycle);
        $("#dropStartHour").attr("disabled", !isCycle);
        $("#dropStartMinute").attr("disabled", !isCycle);
        $("#dropStartMinute").attr("disabled", !isCycle);
        $("#dropEndHour").attr("disabled", !isCycle);
        $("#dropEndMinute").attr("disabled", !isCycle);
    }

    //是否有结束时间点击事件
    function EndDate_Click(isEndDate) {
        $("#txtEndDate").attr("disabled", !isEndDate);
    }

    //验证调度信息
    function Validate() {
        var result = true;
        var alertInfo = "";

        if ($("#txtTimingName").val() == "") {
            result = false;
            alertInfo = "服务名称为必填项";
        }

        //日
        else if ($("#radlFrequency_0").attr("checked")) {
            //第N天
            var interval = parseInt($("#txtDayInterval").val());
            result = interval > 0 && interval <= 30;
            if (!result) {
                alertInfo = "日间隔时间不在许可范围内";
            }
        }
        //周
        else if ($("#radlFrequency_1").attr("checked")) {
            //第N周
            var interval = parseInt($("#txtWeekInterval").val());
            result = interval > 0 && interval <= 30;
            if (!result) {
                alertInfo = "周间隔时间不在许可范围内";
            }
            else {
                result = false;

                //星期选中
                for (var i = 0; i < 7; i++) {
                    if ($("#week_" + i).attr("checked")) {
                        result = true;
                        break;
                    }
                }
                if (!result) {
                    alertInfo = " 必须至少选择一周中的某星期";
                }
            }
        }
        //月
        else if ($("#radlFrequency_2").attr("checked")) {
            //是否相对时间
            if ($("#radNoRelative").attr("checked")) {
                var interval = parseInt($("#txtMonthDay").val());
                result = interval > 0 && interval <= 31;
                interval = parseInt($("#txtMonthInterval").val());
                result = interval > 0 && interval <= 12;
            }
            else {
                var interval = parseInt($("#txtMonthInterval").val());
                result = interval > 0 && interval <= 12;
            }
            if (!result) {
                alertInfo = "月间隔时间不在许可范围内";
            }
        }

        //每天频率验证
        if (result) {
            if ($("#radDayCycle").attr("checked")) {
                var interval = parseInt($("#txtInterval").val());
                result = interval > 0 && interval <= 99;
                if (!result) {
                    alertInfo = "发生周期间隔时间不在许可范围内";
                }
                else {
                    if ($("#dropEndHour").val() == 0 && $("#dropEndMinute").val() == 0) {
                        result = false;
                        alertInfo = "结束时间不能为00:00";
                    }
                }
            }
        }

        if (!result) {
            alert(alertInfo);
        }

        return result;
    }

</script>
<form name="frmInput" id="frmInput" method=post action="@Url.Action(@ViewContext.RouteData.Values["Action"].ToString())" >
<table class="tbl-input" cellspacing="0">
    <colgroup>
        <col style="width: 100px;" />
        <col />
        <col style="width: 70px;" />
        <col class="w2" />
    </colgroup>
    <tbody>
        <tr>
            <th>
                服务名称：
            </th>
            <td colspan="2">
                @Html.TextBoxFor(t => t.ServiceName, new { style = "width:90%" })
            </td>
            <td class="slight">
                <span class="btn-infomust">必填项</span>
            </td>
        </tr>
        <tr>
            <th>
                优先级：
            </th>
            <td>
                <select name="PriorityCodeID" id="PriorityCodeID">
                    <option value="0" @(Model.PriorityCodeID == 0 ? "selected='selected'" : "") >最低</option>
                    <option value="1" @(Model.PriorityCodeID == 1 ? "selected='selected'" : "") >低于普通</option>
                    <option value="2" @(Model.PriorityCodeID == 2 ? "selected='selected'" : "") >普通</option>
                    <option value="3" @(Model.PriorityCodeID == 3 ? "selected='selected'" : "") >高于普通</option>
                    <option value="4" @(Model.PriorityCodeID == 4 ? "selected='selected'" : "") >最高</option>
                </select>
            </td>
            <th>
                出错警告：
            </th>
            <td>
                <select name="AlarmCodeID" id="AlarmCodeID">
                    <option value="0" @(Model.AlarmCodeID == 0 ? "selected='selected'" : "") >无</option>
                    <option value="1" @(Model.AlarmCodeID == 1 ? "selected='selected'" : "") >短信告警</option>
                    <option value="2" @(Model.AlarmCodeID == 2 ? "selected='selected'" : "") >邮件告警</option>
                </select>
            </td>
        </tr>
        <tr>
            <th>
            </th>
            <td colspan="3">
                <input type="checkbox" value="1" id="chkEnableTrace" name="IsEnableTrace" @(Model.IsEnableTrace == 1 ? "checked='checked'" : "")  />
                <label for="chkEnableTrace">
                    是否记录跟踪</label>
                <input type="checkbox" value="1" id="chkEnable" name="StateCodeID" @(Model.StateCodeID == 1 ? "checked='checked'" : "")  />
                <label for="chkEnable">
                    是否启用</label>
                <input type="checkbox" value="1" id="chkRunMiss" name="IsRunMiss" @(Model.IsRunMiss == 1 ? "checked='checked'" : "")  />
                <label for="chkRunMiss">
                    是否运行错过服务</label>
            </td>
        </tr>
        <tr>
            <th>
                调度配置：
            </th>
            <td style="vertical-align: top;" colspan="3">
                <fieldset style="width: 60px; float: left; vertical-align: top; height: 95px; border: 1px solid #ccc;
                    margin: 5px;">
                    <legend style="margin: 2px 5px; font-weight: normal;">发生频率</legend>
                    @Html.RadioButton("Frequency", 0, config.Frequency == XStudio.Service.Timing.FrequencyCode.Day, new { onclick = "Frequency_Click(this.value)", id = "radlFrequency_0" })
                    <label for="radlFrequency_0">
                        每天</label>
                    @Html.RadioButton("Frequency", 1, config.Frequency == XStudio.Service.Timing.FrequencyCode.Week, new { onclick = "Frequency_Click(this.value)", id = "radlFrequency_1" })
                    <label for="radlFrequency_1">
                        每周</label>
                    @Html.RadioButton("Frequency", 2, config.Frequency == XStudio.Service.Timing.FrequencyCode.Month, new { onclick = "Frequency_Click(this.value)", id = "radlFrequency_2" })
                    <label for="radlFrequency_2">
                        每月</label>
                </fieldset>
                <fieldset style="width: 350px; float: left; vertical-align: top; height: 95px; border: 1px solid #ccc;
                    margin: 5px;">
                    <legend style="margin: 2px 5px; font-weight: normal;" id="legFrequencyTitle">每天</legend>
                    <div id="phlDay" style="margin: 5px 5px;">
                        @{var isDay = config.Frequency == XStudio.Service.Timing.FrequencyCode.Day;}
                        <span>每</span>
                        @Html.TextBox("DayInterval", isDay ? config.DayConfig.Interval.ToString() : "", new { id = "txtDayInterval", maxlength = "2", style = "width:20px;" })
                        <span>天</span>
                        <div class="btn-infomust" style="margin-top: 5px;">
                            必填项，范围为1-30</div>
                    </div>
                    <div id="phlWeek" style="margin: 0px 5px; display: none;">
                        @{var isWeek = config.Frequency == XStudio.Service.Timing.FrequencyCode.Week;}
                        <span>每</span>
                        @Html.TextBox("WeekInterval", isWeek ? config.WeekConfig.Interval.ToString() : "1", new { id = "txtWeekInterval", maxlength = "2", style = "width:20px;" })
                        <span>周，在：</span> <span class="btn-infomust" style="margin-top: 5px;">必填项，范围为1-30，至少选择一个星期</span>
                        <input id="week_1" type="checkbox" value="1" @(isWeek && config.WeekConfig.WeekList.Contains(DayOfWeek.Monday) ? "checked=checked" : "")  name="Week" />
                        <label for="week_1">
                            星期一</label>
                        <input id="week_2" type="checkbox" value="2" @(isWeek && config.WeekConfig.WeekList.Contains(DayOfWeek.Tuesday) ? "checked=checked" : "") name="Week" />
                        <label for="week_2">
                            星期二</label>
                        <input id="week_3" type="checkbox" value="3" @(isWeek && config.WeekConfig.WeekList.Contains(DayOfWeek.Wednesday) ? "checked=checked" : "") name="Week" />
                        <label for="week_3">
                            星期三</label>
                        <input id="week_4" type="checkbox" value="4" @(isWeek && config.WeekConfig.WeekList.Contains(DayOfWeek.Thursday) ? "checked=checked" : "") name="Week" />
                        <label for="week_4">
                            星期四</label>
                        <input id="week_5" type="checkbox" value="5" @(isWeek && config.WeekConfig.WeekList.Contains(DayOfWeek.Friday) ? "checked=checked" : "") name="Week" />
                        <label for="week_5">
                            星期五</label>
                        <input id="week_6" type="checkbox" value="6" @(isWeek && config.WeekConfig.WeekList.Contains(DayOfWeek.Saturday) ? "checked=checked" : "") name="Week" />
                        <label for="week_6">
                            星期六</label>
                        <input id="week_0" type="checkbox" value="0" @(isWeek && config.WeekConfig.WeekList.Contains(DayOfWeek.Sunday) ? "checked=checked" : "") name="Week" />
                        <label for="week_0">
                            星期日</label>
                    </div>
                    <div id="phlMonth" style="margin: 0px 5px; display: none;">
                        @{var isMonth = config.Frequency == XStudio.Service.Timing.FrequencyCode.Month;}
                        @{var isMonthRelative = isMonth && config.MonthConfig.IsRelativeDate; }
                        <div style="margin: 5px;">
                            <input type="radio" id="radNoRelative" onclick="Relative_Click(false);"  @(!isMonthRelative ? "checked=checked" : "") value=0 name="IsRelative" />
                            <label for="radNoRelative">
                                第</label>
                            @Html.TextBox("MonthDay", isMonth ? config.MonthConfig.Interval.ToString() : "1", new { id = "txtMonthDay", maxlength = "2", style = "width:20px;" })
                            <span>天，每</span>
                            @Html.TextBox("MonthInterval", isMonth ? config.MonthConfig.Interval.ToString() : "1", new { id = "txtMonthInterval", maxlength = "2", style = "width:20px;" })
                            <span>月</span>
                        </div>
                        <div style="margin: 0px 5px;">
                            <input type="radio" id="radRelative" onclick="Relative_Click(true);" @(isMonthRelative ? "checked=checked" : "") value=1  name="IsRelative" />
                            <label for="radRelative">
                                第</label>
                            <select name="RelativeType" id="dropRelative" width="70px">
                                <option value="1" @((isMonthRelative && config.MonthConfig.RelativeType == XStudio.Service.Timing.RelativeType.First) ? "selected='selected'" : "") >
                                    一个</option>
                                <option value="2" @((isMonthRelative && config.MonthConfig.RelativeType == XStudio.Service.Timing.RelativeType.Second) ? "selected='selected'" : "") >
                                    二个</option>
                                <option value="3" @((isMonthRelative && config.MonthConfig.RelativeType == XStudio.Service.Timing.RelativeType.Third) ? "selected='selected'" : "") >
                                    三个</option>
                                <option value="4" @((isMonthRelative && config.MonthConfig.RelativeType == XStudio.Service.Timing.RelativeType.Fourth) ? "selected='selected'" : "") >
                                    四个</option>
                                <option value="5" @((isMonthRelative && config.MonthConfig.RelativeType == XStudio.Service.Timing.RelativeType.Lasted) ? "selected='selected'" : "") >
                                    末一个</option>
                            </select>
                            <select name="WeenInMonth" id="dropWeenInMonth" width="75px">
                                <option value="1" @((isMonthRelative && config.MonthConfig.RelativeDay == XStudio.Service.Timing.RelativeDay.Monday) ? "selected='selected'" : "") >
                                    星期一</option>
                                <option value="2" @((isMonthRelative && config.MonthConfig.RelativeDay == XStudio.Service.Timing.RelativeDay.Tuesday) ? "selected='selected'" : "") >
                                    星期二</option>
                                <option value="3" @((isMonthRelative && config.MonthConfig.RelativeDay == XStudio.Service.Timing.RelativeDay.Wednesday) ? "selected='selected'" : "") >
                                    星期三</option>
                                <option value="4" @((isMonthRelative && config.MonthConfig.RelativeDay == XStudio.Service.Timing.RelativeDay.Thursday) ? "selected='selected'" : "") >
                                    星期四</option>
                                <option value="5" @((isMonthRelative && config.MonthConfig.RelativeDay == XStudio.Service.Timing.RelativeDay.Friday) ? "selected='selected'" : "") >
                                    星期五</option>
                                <option value="6" @((isMonthRelative && config.MonthConfig.RelativeDay == XStudio.Service.Timing.RelativeDay.Saturday) ? "selected='selected'" : "") >
                                    星期六</option>
                                <option value="0" @((isMonthRelative && config.MonthConfig.RelativeDay == XStudio.Service.Timing.RelativeDay.Sunday) ? "selected='selected'" : "") >
                                    星期日</option>
                                <option value="7" @((isMonthRelative && config.MonthConfig.RelativeDay == XStudio.Service.Timing.RelativeDay.Day) ? "selected='selected'" : "") >
                                    天</option>
                                <option value="8" @((isMonthRelative && config.MonthConfig.RelativeDay == XStudio.Service.Timing.RelativeDay.WorkDay) ? "selected='selected'" : "") >
                                    工作日</option>
                            </select>
                            <span>每</span>
                            @Html.TextBox("MonthInterval2", isMonth ? config.MonthConfig.Interval.ToString() : "1", new { id = "txtMonthInterval2", maxlength = "2", style = "width:20px;" })
                            <span>月</span>
                        </div>
                        <div class="btn-infomust" style="margin: 0px 5px 5px 10px;">
                            必填项，天范围为1-31，月范围1-12</div>
                    </div>
                </fieldset>
                <br />
                <fieldset style="width: 420px; vertical-align: top; height: 110px; border: 1px solid #ccc;
                    margin: 0px 5px;">
                    <legend style="margin: 5px 5px; font-weight: normal;">每日频率</legend>
                    <div style="margin: 0px 5px;">
                        <input type="radio" id="radDayOnce" value=0 onclick="DayFrequency_Click(false);"  @(config.DayInterval.DayFrequency == XStudio.Service.Timing.DayFrequency.Once ? "checked=checked" : "") name="DayFrequency" />
                        <label for="radDayOnce">
                            一次发生于：
                        </label>
                        <select id="dropDayOnceHour" name="DayOnceHour" style="width: 40px">
                            @for (int i = 0; i < 24; i++)
                            {
                                <option id="@i" @( config.DayInterval.DayFrequency == XStudio.Service.Timing.DayFrequency.Once && config.DayInterval.ExecTime.Hours == i ? "selected=selected" : "")  >@i.ToString("00")</option>
                            }
                        </select>
                        <span>：</span>
                        <select id="dropDayOnceMinute" name="DayOnceMinute" style="width: 40px">
                            @for (int i = 0; i < 60; i++)
                            {
                                <option id="@i" @( config.DayInterval.DayFrequency == XStudio.Service.Timing.DayFrequency.Once && config.DayInterval.ExecTime.Minutes == i ? "selected=selected" : "") >@i.ToString("00")</option>
                            }
                        </select>
                    </div>
                    <div style="margin: 2px 5px; width: 343px;">
                        <input type="radio" value=1 id="radDayCycle" @(config.DayInterval.DayFrequency == XStudio.Service.Timing.DayFrequency.Cycle ? "checked=checked" : "") style="float: left;" onclick="DayFrequency_Click(true);" name="DayFrequency" />
                        <div style="text-align: right;">
                            <label for="radDayCycle">
                                &nbsp;发生周期：</label>
                            @Html.TextBox("Interval", config.DayInterval.Interval <= 0 ? "5" : config.DayInterval.Interval.ToString(), new { maxlength = "2", id = "txtInterval", style = "width:20px;" })
                            <select name="IntervalUnit" id="dropUnits" width="55px">
                                <option value="0" @( config.DayInterval.IntervalUnits == XStudio.Service.Timing.IntervalUnits.Second ? "selected=selected" : "") >
                                    秒</option>
                                <option value="1" @( config.DayInterval.IntervalUnits == XStudio.Service.Timing.IntervalUnits.Minute ? "selected=selected" : "") >
                                    分钟</option>
                                <option value="2" @( config.DayInterval.IntervalUnits == XStudio.Service.Timing.IntervalUnits.Hour ? "selected=selected" : "") >
                                    小时</option>
                            </select>
                            <span>起始于：</span>
                            <select id="dropStartHour" name="StartHour" style="width: 40px">
                                @for (int i = 0; i < 24; i++)
                                {
                                    <option id="@i" @( config.DayInterval.StartTime.Hours == i ? "selected=selected" : "") >@i.ToString("00")</option>
                                }
                            </select>
                            <span>：</span>
                            <select id="dropStartMinute" name="StartMinute" style="width: 40px">
                                @for (int i = 0; i < 60; i++)
                                {
                                    <option id="@i" @( config.DayInterval.StartTime.Minutes == i ? "selected=selected" : "") >@i.ToString("00")</option>
                                }
                            </select>
                        </div>
                        <div style="text-align: right; margin-top: 2px;">
                            <span>结束于：</span>
                            <select id="dropEndHour" name="EndHour" style="width: 40px">
                                @for (int i = 0; i < 24; i++)
                                {
                                    <option id="@i" @( config.DayInterval.EndTime.Hours == i ? "selected=selected" : "") >@i.ToString("00")</option>
                                }
                            </select>
                            <span>：</span>
                            <select id="dropEndMinute" name="EndMinute" style="width: 40px">
                                @for (int i = 0; i < 60; i++)
                                {
                                    <option id="@i" @( config.DayInterval.EndTime.Minutes == i ? "selected=selected" : "") >@i.ToString("00")</option>
                                }
                            </select>
                        </div>
                    </div>
                </fieldset>
                <fieldset style="width: 420px; vertical-align: top; height: 70px; border: 1px solid #ccc;
                    margin: 5px;">
                    <legend style="margin: 1px 5px; font-weight: normal;">持续时间</legend>
                    <div style="width: 180px; float: left; margin-top: 2px; margin-left: 5px;">
                        <span>开始日期：</span>
                        <input type="text" value="@config.StartDate.ToString("yyyy-MM-dd")" style="width: 100px;" id="txtStart" name="StartDate" onfocus="WdatePicker({ readOnly: true, skin: 'whyGreen', dateFmt: 'yyyy-MM-dd' })" />
                    </div>
                    <div style="float: left; margin-top: 5px; line-height: 150%;">
                        @{ var hasEnd = config.EndDate.Year > 2000;
                           if (!hasEnd) { config.EndDate = DateTime.Now.AddYears(3); }}
                        <input type="radio" id="radEndDate" onclick="EndDate_Click(true);" @(hasEnd ? "checked=checked" : "") value="1" name="HasEndDate" />
                        <label for="radEndDate">
                            结束日期：</label>
                        <input type="text" value="@config.EndDate.ToString("yyyy-MM-dd")" style="width: 100px;" id="txtEndDate" name="EndDate" onfocus="WdatePicker({ readOnly: true, skin: 'whyGreen', dateFmt: 'yyyy-MM-dd' })" />
                        <br />
                        <input type="radio" id="radNoEndDate" @(!hasEnd ? "checked=checked" : "") onclick="EndDate_Click(false);" value="1" name="HasEndDate" />
                        <label for="radNoEndDate">
                            无结束日期</label>
                    </div>
                </fieldset>
            </td>
        </tr>
        <tr>
            <th>
                备注：
            </th>
            <td colspan="2">
                &nbsp;@Html.TextAreaFor(t => t.Remark, new { rows = 2, style = "width:90%;" })
            </td>
            <td class="slight">
                <span class="btn-info">选填项</span>
            </td>
        </tr>
        <tr>
            <td colspan="4" style="text-align: center;">
                <p class="op">
                    <input type="button" id="btnSave" class="btn-8" value="保存" />
                    <input type="button" value="返回" onclick="$.closeWin(this);" class="btn-8" />
                </p>
            </td>
        </tr>
    </tbody>
</table>
</form>
